class SignupController < ApplicationController
  def school
  end

  def individual
    if params[:v]
      code = PurchaseCode.all code: params[:o]
      ok = code.length > 0
      unless ok
        respond_to do |format|
          format.json { render :json => { :purchased => false } }
        end
        return
      end
      user = User.new
      user.un = params[:u]
      user.pw = params[:p]
      user.rn = params[:r]
      user.em = params[:e]
      user_lang = UserLang.new
      if params[:l].length > 2
        l = Language.all(name: params[:l])[0].id
      else
        l = params[:l]
      end
      user_lang.la = l
      user_lang.cl = params[:c]
      user.user_lang << user_lang
      ok = user.save
      respond_to do |format|
        format.json { render :json => { :saved => ok } }
      end
    else
      l = Language.all
      l.each.with_index do |la, i|
        l[i] = '["' + la.id + '","' + la.name + '"]'
      end
      @languages = l.join ","
      @languages = "[" + @languages + "]"

      c = MongoMapper.database.collection("vocabs").distinct "cl"
      c.each.with_index do |cl, i|
        c[i] = '"' + c[i] + '"'
      end
      @classes = "[" + c.join(',') + "]"
    end
  end

end
